package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.entity.app.PxAdversterSlot;
import com.iot08.common.entity.app.PxAdvertConfiguration;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/13 10:26
 **/
@Slf4j
@Service
public class PxAdversterConfigurationService extends BaseService<PxAdvertConfiguration> {

    @Resource
    private OperationService operationService;

    /**
     * @description 查询广告配置信息
     * @author cwj
     * @param [page]
     * @return org.springframework.data.domain.Page<com.iot08.common.entity.app.PxAdvertConfiguration>
     * @date @time 2025/4/13 10:54
     **/
    public Page<PxAdvertConfiguration> list(PageVO page) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("createTime")));
        pageable.setPage(page);
        Query query = new Query();
        long count = this.count(query);
        page.setTotalResult((int) count);
        List<PxAdvertConfiguration> list = this.find(query.with(pageable));
        Page<PxAdvertConfiguration> result = new PageImpl<PxAdvertConfiguration>(list, pageable, count);
        return result;
    }


    /**
     * @description 添加广告
     * @author cwj
     * @param [pxAdvertConfiguration, user, ip]
     * @return void
     * @date @time 2025/4/13 11:39
     **/
    public void add(PxAdvertConfiguration pxAdvertConfiguration, SysUser user, String ip) {
        if (!ObjectUtils.isEmpty(pxAdvertConfiguration)  && getByTitile(pxAdvertConfiguration.getTitle()) == null) {
            pxAdvertConfiguration.setCreator(user.getAccount());
            pxAdvertConfiguration.setUpdator(user.getAccount());
            pxAdvertConfiguration.setCreateTime(new Date());
            pxAdvertConfiguration.setUpdateTime(new Date());
            this.save(pxAdvertConfiguration);
            operationService.add(user, ip, "广告配置", "添加",
                    String.format("添加广告配置: %s ", pxAdvertConfiguration.getTitle()));
        }
    }
    
    /**
     * @description 查询相同title的广告信息
     * @author cwj
     * @param [title]
     * @return com.iot08.common.entity.app.PxAdvertConfiguration
     * @date @time 2025/4/13 14:27
     **/
    public PxAdvertConfiguration getByTitile(String title) {
        return this.findOne(new Query(Criteria.where("title").is(title)));
    }

    /**
     * @description 查询相同title的其它广告信息
     * @author cwj
     * @param [title, id]
     * @return com.iot08.common.entity.app.PxAdvertConfiguration
     * @date @time 2025/4/13 17:07
     **/
    public PxAdvertConfiguration getByTitileAndId(String title, String id) {
        return this.findOne(new Query(Criteria.where("_id").ne(new ObjectId(id)).and("title").is(title)));
    }

    /**
     * @description 更新广告信息
     * @author cwj
     * @param [pxAdvertConfiguration, user, ip]
     * @return void
     * @date @time 2025/4/13 17:50
     **/
    public void update(PxAdvertConfiguration pxAdvertConfiguration, SysUser user, String ip) {
        //APP标识不能重复
        if(ObjectUtils.isEmpty(getByTitileAndId(pxAdvertConfiguration.getTitle(),pxAdvertConfiguration.getId()))){
            Update update = new Update();
            update.set("title", pxAdvertConfiguration.getTitle());
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getAdversterId())) {
                update.set("adversterId", pxAdvertConfiguration.getAdversterId());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getAdvertsterName())) {
                update.set("advertsterName",pxAdvertConfiguration.getAdvertsterName());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getAdversterSlotId())) {
                update.set("adversterSlotId",pxAdvertConfiguration.getAdversterSlotId());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getAdversterSlotName())) {
                update.set("adversterSlotName",pxAdvertConfiguration.getAdversterSlotName());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getTitle())) {
                update.set("title",pxAdvertConfiguration.getTitle());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getContent())) {
                update.set("content",pxAdvertConfiguration.getContent());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getImageUrl())) {
                update.set("imageUrl",pxAdvertConfiguration.getImageUrl());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getRedirectUrl())) {
                update.set("redirectUrl",pxAdvertConfiguration.getRedirectUrl());
            }
            if(!ObjectUtils.isEmpty(pxAdvertConfiguration.getRedirectType())) {
                update.set("redirectType",pxAdvertConfiguration.getRedirectType());
            }
            if(!StringUtils.isEmpty(pxAdvertConfiguration.getVersion())) {
                update.set("version",pxAdvertConfiguration.getVersion());
            }
            if(!ObjectUtils.isEmpty(pxAdvertConfiguration.getWeight())) {
                update.set("weight", pxAdvertConfiguration.getWeight());
            }
            if(!ObjectUtils.isEmpty(pxAdvertConfiguration.getPriority())) {
                update.set("priority", pxAdvertConfiguration.getPriority());
            }
            if(!ObjectUtils.isEmpty(pxAdvertConfiguration.getStatus())) {
                update.set("status", pxAdvertConfiguration.getStatus());
            }
            if(!ObjectUtils.isEmpty(pxAdvertConfiguration.getUserType())) {
                update.set("userType", pxAdvertConfiguration.getUserType());
            }
            update.set("updator",user.getAccount());
            update.set("updateTime", new Date());
            this.updateById(new ObjectId(pxAdvertConfiguration.getId()), update);
            operationService.add(user, ip, "广告配置", "编辑",
                    String.format("修改运营版广告配置: %s ", pxAdvertConfiguration.getTitle()));
        }
    }

    /**
     * @description 更新广告的广告位名称
     * @author cwj
     * @param [slotId, slotName]
     * @return void
     * @date @time 2025/4/14 17:27
     **/
    public void updateSlotNameBySlotId(String slotId,String slotName) {
        Query query = new Query(Criteria.where("adversterSlotId").is(slotId));
        Update update = new Update();
        update.set("adversterSlotName",slotName);
        mongoTemplate.updateMulti(query,update,getTClass());
    }

    /**
     * @description 更新广告商的名称
     * @author cwj
     * @param [adversterId, adversterName]
     * @return void
     * @date @time 2025/4/14 17:59
     **/
    public void updateAdversterNameByAdversterId(String adversterId,String adversterName){
        Query query = new Query(Criteria.where("adversterId").is(adversterId));
        Update update = new Update();
        update.set("advertsterName",adversterName);
        mongoTemplate.updateMulti(query,update,getTClass());
    }
}
